﻿namespace PugLib.UI.Controls
{
	using System;
	using System.Collections.Generic;
	using System.Drawing;
	using System.Linq;
	using System.Threading;
	using System.Windows.Forms;

	using PugLib.Extensions.Collections;

	public sealed partial class PugDialog : PugForm, IPugDialogResult
	{
		private PugDialogImage _dialogImage = PugDialogImage.None;

		public PugDialog()
		{
			InitializeComponent();
		}

		public PugDialogImage DialogImage
		{
			get { return _dialogImage; }
			set
			{
				_dialogImage = value;
				switch (_dialogImage)
				{
					default:
						SetImage(null);
						break;
					case PugDialogImage.Delete:
						SetImage(Images.Delete);
						break;
					case PugDialogImage.Exclamation:
					case PugDialogImage.Warning:
						SetImage(Images.Important);
						break;
					case PugDialogImage.Globe:
						SetImage(Images.Globe);
						break;
					case PugDialogImage.Help:
						SetImage(Images.Help);
						break;
					case PugDialogImage.Information:
					case PugDialogImage.Error:
						SetImage(Images.Info);
						break;
				}
			}
		}

		#region IPugDialogResult Members

		public PugDialogResult Result { get; set; }

		#endregion

		public void SetImage(Image img)
		{
			pbIcon.Image = img;
		}

		public void SetMessage(string msg)
		{
			lblMessage.Text = msg;
		}

		public void SetCaption(string caption)
		{
			Text = caption;
		}

		public void AddButton(PugDialogButton newButton)
		{
			newButton.Dock = DockStyle.Top;
			pnlButtons.Controls.Add(newButton);
		}

		public void AddButtons(IEnumerable<PugDialogButton> newButtons)
		{
			PugDialogButton[] ham = newButtons.ToArray();
			ham.ForEach(b => b.Dock = DockStyle.Top);

			ham.ForEachBackwards(pnlButtons.Controls.Add);
		}

		private void PugDialogLoad(object sender, EventArgs e)
		{
			Point startCorner = System.Windows.Forms.Cursor.Position;
			startCorner.X -= Width / 2;
			Location = startCorner;
		}

		public void TestShow()
		{
			Show();
			Thread.Sleep(1000);
			Close();
		}
	}
}